package com.yixun.lbx.service.impl;

import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.entity.YxProCompanyProduct;
import com.yixun.lbx.mapper.company.CompanyMapper;
import com.yixun.lbx.mapper.company.CompanyProductMapper;
import com.yixun.lbx.service.CompanyProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class CompanyProductServiceImpl implements CompanyProductService {
    private final CompanyProductMapper companyProductMapper;
    private final CompanyMapper companyMapper;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public CompanyProductServiceImpl(CompanyProductMapper companyProductMapper, CompanyMapper companyMapper) {
        this.companyProductMapper = companyProductMapper;
        this.companyMapper = companyMapper;
    }

    /*
     * 添加 企业相关 照片
     */
    @Override
    public int addCompanyProduct(HashMap<String, Object> map) {
        map.put("addTime", dateFormat.format(new Date()));
        int i = companyProductMapper.insetCompanyProduct(map);
        return i;
    }

    /*
     * 更新 企业照片
     * */
    @Override
    public int updateCompanyProduct(HashMap<String, Object> map) {
        map.put("updateTime", dateFormat.format(new Date()));
        int i = companyProductMapper.updateCompanyProduct(map);
        return i;
    }

    /*
     * 删除 status字段设置 1 无效
     * */
    @Override
    public int deleteCompanyProduct(HashMap<String, Object> map) {
        String updateTime = dateFormat.format(new Date());
        map.put("updateTime", updateTime);
        String[] ids = (String[]) map.get("ids");
        Integer status = companyProductMapper.getOneById(ids[0]).getStatus();
        if (status==1){
            map.put("status",0);
        }else {
            map.put("status",1);
        }
        int i = companyProductMapper.deleteCompanyProduct(map);
        return i;
    }

    /*
     * 通过字段 查询CompanyProduct 该字段可以为任何一个或者多个字段
     */
    @Override
    public List<YxProCompanyProduct> getCompanyProductByArgs(YxProCompanyProduct companyProduct) {
        return companyProductMapper.getCompanyProductByArgs(companyProduct);
    }

    @Override
    public Integer CpCount(HashMap<String, String> map) {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(map.get("code"));
        YxProCompany company = companyMapper.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            map.put("qyId", String.valueOf(company.getQyId()));
        }
        return companyProductMapper.getCount(map);
    }

    @Override
    public List<YxProCompanyProduct> getListByPage(HashMap<String, String> map) {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(map.get("code"));
        YxProCompany company = companyMapper.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            map.put("qyId", String.valueOf(company.getQyId()));
        }
        return companyProductMapper.getListByPage(map);
    }


    @Override
    public List<YxProCompanyProduct> getSearchListByPage(HashMap<String, String> map) {
        return companyProductMapper.getSearchListByPage(map);
    }
    
    @Override
    public YxProCompanyProduct getOneById(String id) {
        return companyProductMapper.getOneById(id);
    }
}
